<template>
    <div>
        <Head></Head>
        <el-form ref="form" :model="form" label-width="80px">
            <el-form-item label="资质图片">
                <el-upload
                        class="avatar-uploader1"
                        action="api/userInfo/img"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess1"
                        :before-upload="beforeAvatarUpload1">
                    <img v-if="imageUrl1" :src="imageUrl1" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="身份证图片">
                <el-upload
                        class="avatar-uploader"
                        action="api/userInfo/img1"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess2"
                        :before-upload="beforeAvatarUpload2">
                    <img v-if="imageUrl2" :src="imageUrl2" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
            <el-form-item label="公司名字">
                <el-input v-model="form.userInfoCompanyName"></el-input>
            </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
            <el-button @click="upSubmit">取消</el-button>
        </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import Head from "../cart/head"
    import {examine} from "../../service/user"
    import { mapState} from "vuex";
    export default {
        // eslint-disable-next-line vue/no-unused-components
        components: {Head},
        data() {
            return {
                form: {
                    userInfoCompanyName: '',
                    userId: ''
                },
                imageUrl1: '',
                imageUrl2: '',
            }
        },

        computed: {
            ...mapState("cart",["user"])
        },
        methods: {
            handleAvatarSuccess1(res, file) {
                this.imageUrl1 = URL.createObjectURL(file.raw);
            },
            handleAvatarSuccess2(res, file) {
                this.imageUrl2 = URL.createObjectURL(file.raw);
            },
            beforeAvatarUpload1(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            beforeAvatarUpload2(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            async onSubmit() {
                this.form.userId = this.user.userId;
                await examine(this.form.userInfoCompanyName,this.form.userId);
                alert("提交成功");
                this.$router.push("/items");
            },
            upSubmit(){
                this.$router.push("/items");
            },
        },
    }
</script>

<style scoped>
    .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }
    .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 178px;
        height: 178px;
        line-height: 178px;
        text-align: center;
    }
    .avatar {
        width: 178px;
        height: 178px;
        display: block;
    }
</style>